<?php


require('mc_table.php');

//koneksi ke database
$host = "localhost";
$user = "root";
$pass = "";
$dbnm = "db_rental_dev";
 
$conn = mysql_connect($host, $user, $pass);
if ($conn) {
	$open = mysql_select_db($dbnm);
	if (!$open) {
		die ("Database tidak dapat dibuka karena ".mysql_error());
	}
} else {
	die ("Server MySQL tidak terhubung karena ".mysql_error());
}
//akhir koneksi

function DateToIndo($date) { // fungsi atau method untuk mengubah tanggal ke format indonesia
   // variabel BulanIndo merupakan variabel array yang menyimpan nama-nama bulan
		$BulanIndo = array("Januari", "Februari", "Maret",
						   "April", "Mei", "Juni",
						   "Juli", "Agustus", "September",
						   "Oktober", "November", "Desember");
	
		$tahun = substr($date, 0, 4); // memisahkan format tahun menggunakan substring
		$bulan = substr($date, 5, 2); // memisahkan format bulan menggunakan substring
		$tgl   = substr($date, 8, 2); // memisahkan format tanggal menggunakan substring
		
		$result = $tgl . " " . $BulanIndo[(int)$bulan-1] . " ". $tahun;
		return($result);
}

function DateToIndoAngka($date) { // fungsi atau method untuk mengubah tanggal ke format indonesia
   // variabel BulanIndo merupakan variabel array yang menyimpan nama-nama bulan
	
		$tahun = substr($date, 0, 4); // memisahkan format tahun menggunakan substring
		$bulan = substr($date, 5, 2); // memisahkan format bulan menggunakan substring
		$tgl   = substr($date, 8, 2); // memisahkan format tanggal menggunakan substring
		
		$result = $tgl."/".$bulan."/".$tahun;
		return($result);
}


//get query dan variabel
$mulai = $_GET['mulai'];
$akhir = $_GET['akhir'];
$gudang = $_GET['gudang'];
$decode_cr = $_GET['cr_txt'];
$getquery = str_replace("+"," ",$_GET[query]);
$getquerykorporate = str_replace("+"," ",$_GET[query_korporate]);

//instansiasi fpdf
$pdf = new PDF_MC_Table('L','cm','Legal');
$pdf->AliasNbPages();  
$pdf->AddPage();
$pdf->SetMargins(0.5,0.7,0.7);  

$sql = "SELECT gudang FROM gl_gudang WHERE id = '$gudang' ";
$cekdata = mysql_query($sql) or die(mysql_error());
$row_data = mysql_fetch_array($cekdata);

 #tampilkan judul laporan
$pdf->SetFont('Arial','B','16');
$pdf->Cell(0,0, "CIPAGANTI RENTAL CAR & TRAVEL", 0, 1, 'C');
$pdf->Cell(35,2, "PENERIMAAN HASIL SEWA KENDARAAN", 0, 1, 'C');
$pdf->SetFont('Arial','B','12');
$pdf->Cell(3,3, "CABANG : ".$row_data['gudang'],0,0,'L');
$pdf->Cell(31.5,3, "PERIODE : ".DateToIndo($mulai)." - ".DateToIndo($akhir),0,0,'R');


//membuat kop tabel 
$y=$pdf->GetY();  
$pdf->SetY($y+2.5);  
$pdf->SetFont('Arial','B',11);
$pdf->SetWidths(array(2.3, 2.5, 2.5, 2.5, 2.5, 2.2, 2.2, 2.6, 2.3, 2.6, 2.6, 2.6, 2.6, 2.6));
$pdf->SetHeight(0.1);
$pdf->SetAligns(Array('C','C','C','C','C','C','C','C','C','C','C','C','C','C'));
$pdf->Row(array("User", "Nama Customer", "No Invoice", "Periode Awal", "Periode Akhir", "No. Polisi", "Angsuran", "Uang Muka", "Jaminan", "Denda", "Tunai", "CQ/BG/CD", "Operasional", "Total"));

$pdf->SetFont('Arial','B',9);
$pdf->SetWidths(array(34.6));
$pdf->Row(array("SEWA KENDARAAN RETAIL"));


//ISI TABEL
$pdf->SetFont('Arial','',9);
$pdf->SetWidths(array(2.3, 2.5, 2.5, 2.5, 2.5, 2.2, 2.2, 2.6, 2.3, 2.6, 2.6, 2.6, 2.6, 2.6));
$pdf->SetHeight(0.1);
$pdf->SetAligns(Array('C','C','C','C','C','C','C','C','C','C','C','C','C','C'));

$query_data = $getquery;
$query = mysql_query ($query_data) or die("Query gagal");

while($result= mysql_fetch_array( $query )){
	
	$pdf->Row(array($result['user'], $result['customer'], $result['no_ref'], DateToIndoAngka($result['periodeawal']), DateToIndoAngka($result['periodeakhir']), $result['kendaraan'], number_format($result['angsuran'],0,',','.').",-", number_format($result['uangmuka'],0,',','.').",-", "0,-", "0,-", number_format($result['tunai'],0,',','.').",-", number_format($result['transfer'],0,',','.').",-", number_format($result['operasional'],0,',','.').",-", number_format($result['total'],0,',','.').",-" ));
	
	$totalangsuran += $result['angsuran'];
    $totaluangmuka += $result['uangmuka'];
	$totaltrasfer += $result['transfer'];
    $totaltunai += $result['tunai'];
    $totaloperasional += $result['operasional'];
	$total += $result['total'];
	
}

$pdf->SetFont('Arial','B',9);
$pdf->SetWidths(array(14.5,2.2,2.6,2.3, 2.6, 2.6, 2.6, 2.6,2.6));
$pdf->Row(array("SUBTOTAL",number_format($totalangsuran,0,',','.').",-",number_format($totaluangmuka,0,',','.').",-", "0,-", "0,-",number_format($totaltunai,0,',','.').",-",number_format($totaltrasfer,0,',','.').",-", number_format($totaloperasional,0,',','.').",-", number_format($total,0,',','.').",-"));
$pdf->SetWidths(array(34.6));
$pdf->Row(array("SEWA KENDARAAN NON RETAIL (CORPORATE)"));

$pdf->SetFont('Arial','',9);
$pdf->SetWidths(array(2.3, 2.5, 2.5, 2.5, 2.5, 2.2, 2.2, 2.6, 2.3, 2.6, 2.6, 2.6, 2.6, 2.6));
$pdf->SetHeight(0.1);
$pdf->SetAligns(Array('C','C','C','C','C','C','C','C','C','C','C','C','C','C'));

$query_datakorporate = $getquerykorporate;
$querykorp = mysql_query ($query_datakorporate) or die("Query gagal");

while($resultkorp= mysql_fetch_array( $querykorp )){
	
	$pdf->Row(array($resultkorp['user'], $resultkorp['customer'], $resultkorp['no_ref'], DateToIndoAngka($resultkorp['periodeawal']), DateToIndoAngka($resultkorp['periodeakhir']), $resultkorp['kendaraan'], number_format($resultkorp['angsuran'],0,',','.').",-", number_format($resultkorp['uangmuka'],0,',','.').",-", "0,-", "0,-", number_format($resultkorp['tunai'],0,',','.').",-", number_format($resultkorp['transfer'],0,',','.').",-", number_format($resultkorp['operasional'],0,',','.').",-", number_format($resultkorp['total'],0,',','.').",-" ));
	
	$totalangsurankorporate += $resultkorp['angsuran'];
    $totaluangmukakorporate += $resultkorp['uangmuka'];
	$totaltrasferkorporate += $resultkorp['transfer'];
    $totaltunaikorporate += $resultkorp['tunai'];
    $totaloperasionalkorporate += $resultkorp['operasional'];
	$totalkorporate += $resultkorp['total'];
	
}

$pdf->SetFont('Arial','B',9);
$pdf->SetWidths(array(14.5,2.2,2.6,2.3, 2.6, 2.6, 2.6, 2.6,2.6));
$pdf->Row(array("SUBTOTAL",number_format($totalangsurankorporate,0,',','.').",-",number_format($totaluangmukakorporate,0,',','.').",-", "0,-", "0,-",number_format($totaltunaikorporate,0,',','.').",-",number_format($totaltrasferkorporate,0,',','.').",-", number_format($totaloperasionalkorporate,0,',','.').",-", number_format($totalkorporate,0,',','.').",-"));
$pdf->SetWidths(array(34.6));
$pdf->Row(array(""));
$pdf->SetWidths(array(14.5,2.2,2.6,2.3, 2.6, 2.6, 2.6, 2.6,2.6));
$pdf->SetFont('Arial','B',10);
$pdf->Row(array("GRAND TOTAL",number_format($totalangsuran+$totalangsurankorporate,0,',','.').",-",number_format($totaluangmuka+$totaluangmukakorporate,0,',','.').",-","0,-", "0,-",number_format($totaltunai+$totaltunaikorporate,0,',','.').",-",number_format($totaltrasfer+$totaltrasferkorporate,0,',','.').",-",number_format($totaloperasional+$totaloperasionalkorporate,0,',','.').",-",number_format($total+$totalkorporate,0,',','.').",-" ));

$pdf->Ln();
$pdf->Output('Laporan Penerimaan Hasil Sewa Cabang '.$row_data['gudang'].' Periode '.DateToIndo($mulai)." - ".DateToIndo($akhir).'.pdf','D');


?>